const app = getApp();

export const back = (delta) => {
  let page = getCurrentPages();
  if (page.length == 1) {
    uni.switchTab({
      url: "/pages/index/index",
    });
    return false;
  }
  uni.navigateBack({
    delta,
  });
};

export const backtime = (delta = 1) => {
  setTimeout(() => {
    uni.navigateBack({
      delta,
    });
  }, 1000);
};

export const msg = (title, mask) => {
  uni.showToast({
    title,
    mask,
    icon: "none",
  });
};

export const showModal = (data) => {
  const {
    t1 = "提示",
    t2 = "",
    btn1show = true,
    btn1text = "取消",
    btn2text = "确定",
    inputshow = false,
    inputtext = "请输入",
  } = data;
  return new Promise((reslove, reject) => {
    uni.showModal({
      title: t1,
      content: t2,
      showCancel: btn1show,
      cancelText: btn1text,
      confirmText: btn2text,
      editable: inputshow,
      placeholderText: inputtext,
      success(res) {
        if (res.confirm || res.content) {
          reslove(res);
        } else {
          reject(res);
        }
      },
    });
  });
};

export const go = (url) => {
  let tabbar = [
    "/pages/index/index",
    "/pages/cate/cate",
    "/pages/car/car",
    "/pages/user/user",
  ];
  if (tabbar.includes(url)) {
    uni.switchTab({
      url,
    });
  } else {
    uni.navigateTo({
      url,
    });
  }
};

export const loading = (title = "加载中") => {
  uni.showLoading({
    title,
    mask: true,
  });
};

export const getLocation = () => {
  return new Promise((reslove, reject) => {
    // uni.getLocation({
    //     type: 'gcj02',
    //     success(res) {
    //         console.log(res)
    //         console.log('经纬度：' + res.longitude + ',' + res.latitude)
    //         let long = res.longitude
    //         let lat = res.latitude
    //         uni.request({
    //             url: 'https://restapi.amap.com/v3/geocode/regeo?parameters',
    //             method: 'GET',
    //             data: {
    //                 key: 'e09bc0c63623d23fe4c48303930ab550',
    //                 location: long + ',' + lat,
    //                 output: 'JSON'
    //             },
    //             success(res) {
    //                 if (res.data.status == 1) {
    //                     let data = res.data.regeocode.addressComponent
    //                     let province = (Object.prototype.toString.call(data.province) == "[object String]") ? data.province : ''
    //                     let city = (Object.prototype.toString.call(data.city) == "[object String]") ? data.city : data.province
    //                     let district = (Object.prototype.toString.call(data.district) == "[object String]") ?
    //                         data.district : (Object.prototype.toString.call(data.city) == "[object String]") ? data.city : data.province;
    //                     app.globalData.address.province = province
    //                     app.globalData.address.city = city
    //                     app.globalData.address.district = district
    //                     reslove({
    //                         province,
    //                         city,
    //                         district
    //                     })
    //                 } else {
    //                     uni.showModal({
    //                         title: '提示',
    //                         content: '解析地址失败' + res.data.infocode,
    //                         showCancel: false
    //                     })
    //                 }
    //             },
    //             fail(err) {
    //                 console.log(err)
    //                 uni.showModal({
    //                     title: '提示',
    //                     content: '请检查网络是否连接',
    //                     showCancel: false
    //                 })
    //                 reject()
    //             },
    //             complete() {
    //                 uni.hideLoading()
    //                 reject()
    //             }
    //         })
    //     },
    //     fail(err) {
    //         console.log(err)
    //         // reject()
    //         // uni.showModal({
    //         //     title: '提示',
    //         //     content: '获取定位失败,请点击授权地址',
    //         //     success(res) {
    //         //         if (res.confirm) {
    //         //             uni.openSetting();
    //         //         }
    //         //     }
    //         // })
    //     },
    // })
  });
};

export const requestPayment = (data) => {
  //拉起微信支付
  return new Promise((reslove, reject) => {
    uni.requestPayment({
      timeStamp: data.timeStamp,
      nonceStr: data.nonceStr,
      package: data.package,
      signType: data.signType,
      paySign: data.paySign,
      success(res) {
        reslove(res);
      },
      fail(res) {
        reject(res);
      },
      complete() {
        uni.hideLoading();
      },
    });
  });
};

export const getImageInfo = (src) => {
  //获取图片信息
  return new Promise((reslove, reject) => {
    uni.getImageInfo({
      src,
      success(res) {
        reslove(res);
      },
      fail(err) {
        reject(err);
      },
    });
  });
};

export const requestSubscribeMessage = (data) => {
  return new Promise((reslove, reject) => {
    console.log(data.tmplIds);
    uni.requestSubscribeMessage({
      tmplIds: data.tmplIds,
      success(res) {
        console.log(res);
        if (res.errMsg == "requestSubscribeMessage:ok") {
          reslove();
        }
      },
      fail(err) {
        console.log(err);
        reslove();
      },
    });
  });
};
